﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MySoftWare.Component.Tools.Utils
{
    /// <summary>
    /// 对象的验证和转换
    /// </summary>
    public static class ObjUtils
    {
        /// <summary>
        /// 判断对象是否为Int32类型的数字
        /// </summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        public static bool IsNumeric(this object obj)
        {
            if (obj != null)
                return IsNumeric(obj.ToString());

            return false;
        }

        /// <summary>
        /// 是否为Double类型
        /// </summary>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static bool IsDouble(this object obj)
        {
            if (obj != null)
                return Regex.IsMatch(obj.ToString(), @"^([0-9])[0-9]*(\.\w*)?$");

            return false;
        }

        /// <summary>
        /// 将对象转换为Int32类型
        /// </summary>
        /// <param name="expression">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static int StrToInt(this object expression, int defValue)
        {
            if (expression != null)
                return StrToInt(expression.ToString(), defValue);
            return defValue;
        }

        /// <summary>
        /// 将对象转换为大数据
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="defValue"></param>
        /// <returns></returns>
        public static Decimal StrToDecimal(this object expression, Decimal defValue)
        {
            if (expression != null)
                return StrToDecimal(expression.ToString(), defValue);
            return defValue;
        }

        /// <summary>
        /// 将Object型转换为float型
        /// </summary>
        /// <param name="strValue">要转换的字符串</param>
        /// <param name="defValue">缺省值</param>
        /// <returns>转换后的int类型结果</returns>
        public static float StrToFloat(this object strValue, float defValue)
        {
            if (strValue != null)
                return StrToFloat(strValue.ToString(), defValue);
            return defValue;
        }
    }
}
